package com.zjht.gmcc.servlet.pdt;

import java.io.File;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

import com.zjht.gmcc.bean.BaseServlet;
import com.zjht.gmcc.bean.util.MyJDBC;
import com.zjht.gmcc.bean.util.MyParamUtil;
import com.zjht.gmcc.bean.util.MyPath;

public class MydetailApply extends BaseServlet {
	private static final long serialVersionUID = 1L;

	public boolean process(HttpServletRequest request, HttpServletResponse response,
			HttpSession session, PrintWriter out, MyJDBC myJDBC,  String menu)
			throws Exception {
		
		int i_flag=0;
		String paraValue="";
		int ordertypeid = MyParamUtil.getSEQ("SEQ_BUYER");
		
		String name="合同模板"+ordertypeid;
		String saveDirectory = MyPath.PATH_BUY;
		DiskFileUpload fu = new DiskFileUpload();
		fu.setHeaderEncoding("GBK");
		List fileItems = fu.parseRequest(request);
		Iterator iter = fileItems.iterator();
		//取得表单提交的各个参数

		FileItem item1 = (FileItem) fileItems.get(0);
		FileItem item3 = (FileItem) fileItems.get(2);
		String ordername=item1.getString();
		String filepath = item3.getString();
		int length = filepath.length();
		String suffix = filepath.substring(length-4, length).toUpperCase();
		String filename = "合同模板"+ordertypeid+suffix;
		String filename1= "hetongmuban"+ordertypeid+suffix;

		StringBuilder sql = new StringBuilder();
		sql.append ("INSERT INTO ordertype (ordertypeid,ordertypename,ordertypefile,status,orderrealname) VALUES (?,?,?,'1',?)");
		PreparedStatement ps = myJDBC.prepareStatement(sql);
		ps.setInt(1, ordertypeid);
		ps.setString(2, ordername);
		ps.setString(4, filename1);
		while (iter.hasNext()) {
			FileItem item = (FileItem) iter.next();
			if (item.getFieldName().indexOf("ordertypefile") < 0 && item.isFormField()) {
				i_flag = 1;
			} else {
				paraValue = null;
				long size = item.getSize();
				if (size == 0) {// 文件不存在
					// alert(out, "导入文件不存在，请核实！", false);
					// i_flag = 0;
					// continue;
				} else {
					if (size > (2 * 1024 * 1024)) {
						alert(out, "导入文件大小不能超过 2M，请核实！");
						i_flag = 0;
						return false;
					}
					if (filename != null && filename.length() > 0) {
					
							try {
								File fNew = new File(saveDirectory, filename);
								item.write(fNew);
								paraValue = name;
								ps.setString(3, paraValue);
								i_flag = 1;
							} catch (Exception exfile) {
								alert(out, "文件上传有错误，请您重新上传！");
								i_flag = 0;
								return false;
							}
						
					}
				}
			}
		}
		if (i_flag == 1) {// 所有表单属性已提交完成
			i_flag = 0;
			i_flag = ps.executeUpdate();
		}
	
	ps.close();
	ps = null;
	

		
		
		String url = "mydetail.jsp";
		String act = "合同分类添加";
		if (i_flag > 0) {
			alert(out, act + "成功", url);
		} else {
			errAlert(request, out, menu, act + "失败");
		}
		return i_flag > 0;
	}
}
